﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SensitivityTest.model;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;

namespace SensitivityTest.BLL
{
    class BusinessManager
    {
        public double value1, value2;
        SMC100A SIG = new SMC100A();
        FSL fsl = new FSL();
        public  Boolean OpenSig(String sig1,String sig2,out string ErrStr)
        {
            
            if (!SIG.SigInit(sig1, sig2, out ErrStr))
            {
                return false;
            }
            else
            { 
                return true;
            }
        }

        public  Boolean OpenSpec(String sa, out string ErrStr)
        {
            
            if (!fsl.SpecInit(sa, out ErrStr))
            {
                return false;
            }
            else
            {    
                return true;
            }
        }

        public void StartCalibration(bool loadConfig, Frm_Main fmain)
        {
            bool state=true;
            if (loadConfig == false)
            {
                MessageBox.Show("未成功加载配置");
                return;
            }
            Calibration(fmain,out state);
            if (state == false)
            {
                MessageBox.Show("校准失败");
            }
        }

        public void Calibration(Frm_Main fmain ,out bool state)
        {

            fsl.InitParamter_Spec(fmain);                              //设置频谱仪参数
            SIG.InitFreqPow_Sig(1, fmain);                             //初始化信号源1参数
            SIG.SetRFOn(1);                                         //打开信号
           // fsl.SetConOn();                                       //频谱仪开始连续扫描
            fsl.SetMark();                                          //设置marker点
            AdjustPower(1,out state,fmain);                               //信号源功率调整过程
            if (state == false)
            {
                state = false;
                return;
            }
            SIG.SetRFOff(1);                                         //close信号
            SIG.InitFreqPow_Sig(2, fmain);                             //初始化信号源2参数
            SIG.SetRFOn(2);                                         //打开信号
            AdjustPower(2, out state, fmain);                               //信号源功率调整过程
            SIG.SetRFOff(2);                                         //close信号
        }

        public void AdjustPower(int signame, out bool state, Frm_Main fmain) 
        {
            double specValue;
            double StepValue=0;
            bool deta;
            do
            {
                double sigCurrentPower = SIG.PowerGet(signame);
                if (sigCurrentPower > 0)
                {
                    MessageBox.Show("信号源功率超出范围");
                    state = false;
                    return;
                }
                fsl.SingleSweep();                                      //频谱仪开始单次扫描
                fsl.SetMark();
                specValue = fsl.GetMarkVal();                           //获取marker点结果
                if (0 - specValue > 4)
                {
                    StepValue = 2;
                }
                else if (0 - specValue > 2 && (0 - specValue <=4))
                {
                    StepValue = 1;
                }
                else if (0 - specValue > 1 && (0 - specValue <= 2))
                {
                    StepValue = 0.5;
                }
                else if (0 - specValue > 0.2 && (0 - specValue <= 1))
                {
                    StepValue = 0.1;
                }
                else if (0 - specValue > 0 && (0 - specValue <= 0.2))
                {
                    StepValue = 0.02;
                }
                deta = Math.Abs(0 - specValue) > 0.01; //0.01
                if (deta == true)
                {
                    SIG.PowerUpOrDown(signame, (0 - specValue) > 0, StepValue);
                }
            }
            while (deta);
            //SaveSigCalData(signame,fmain);
            value1 = Math.Abs(SIG.PowerGet(1));
            value2 = Math.Abs(SIG.PowerGet(2));
            for (int i = 0; i < fmain.dataGridView1.Rows.Count-1; i++)
            {
                fmain.dataGridView1.Rows[i].Cells["P1"].Value = value1;
                fmain.dataGridView1.Rows[i].Cells["P2"].Value = value2;
            }
            state = true;
        }

        public void SaveSigCalData(int num, Frm_Main fmain)
        {
            double calResule = SIG.PowerGet(num);
            if (!System.IO.Directory.Exists("d:/OIP3校准数据"))
            { 
                System.IO.Directory.CreateDirectory("d:/OIP3校准数据");
            }
            string productName = fmain.cmb_ProductName.Text + ".xml";
            if (!System.IO.File.Exists(productName))
            {
                System.IO.File.Create("d:/OIP3校准数据/" + productName);
            }
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
                XmlElement CalData = xmlDoc.CreateElement("CalData");
                xmlDoc.AppendChild(CalData);
                XmlElement sig1Freq = xmlDoc.CreateElement("sig1Freq");
                XmlElement sig1Power = xmlDoc.CreateElement("sig1Power");
                XmlElement sig2Freq = xmlDoc.CreateElement("sig2Freq");
                XmlElement sig2Power = xmlDoc.CreateElement("sig2Power");
                if (num == 1)
                {
                    sig1Freq.InnerText = (string)fmain.dt.Rows[0]["F1"];
                    sig1Power.InnerText = calResule.ToString();
                }
                else if (num == 2)
                {
                    sig2Freq.InnerText = (string)fmain.dt.Rows[0]["F2"];
                    sig2Power.InnerText =  calResule.ToString();
                }             
                CalData.AppendChild(sig1Freq);
                CalData.AppendChild(sig1Power);
                CalData.AppendChild(sig2Freq);
                CalData.AppendChild(sig2Power);
                xmlDoc.Save("d:/OIP3校准数据/" + productName);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        public void StartTest(Frm_Main fmain)
        {
            double resultValue;
            try
            {
                SIG.SetFreqPower(1, fmain);
                SIG.SetFreqPower(2, fmain);
                fsl.InitParamter_Spec(fmain);                              //设置频谱仪参数
                SIG.SetRFOn(1);
                SIG.SetRFOn(2);
                double resultTemp;
                for (int i = 0; i < fmain.dataGridView1.Rows.Count - 1; i++)
                {
                    MessageBox.Show("请按'" + fmain.dataGridView1.Rows[i].Cells["Chan"].Value + "'通道接线");
                    fsl.SingleSweep();                                      //频谱仪开始单次扫描
                    fsl.SetMark();                                          //设置marker点

                    fsl.SetMarkNext();
                    fsl.SetMarkNext();
                    resultValue = fsl.GetMarkVal();                           //获取marker点结果
                    fsl.SetMarkNext();
                    resultTemp = fsl.GetMarkVal();                           //获取marker点结果
                    if (resultValue < resultTemp)
                    {
                        resultValue = resultTemp;
                    }
                    else
                    {
                        fsl.SetMarkLeft();
                        fsl.SetMarkLeft();
                        fsl.SetMarkLeft();
                    }
                    resultValue = Math.Abs(Math.Round(resultValue / 2, 2));
                    fmain.dataGridView1.Rows[i].Cells["testValue"].Value = resultValue;
                    
                    if (resultValue > 31)
                    {
                        fmain.dataGridView1.Rows[i].Cells["testResult"].Value = "PASS";
                        fmain.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PaleTurquoise; 
                    }
                    else
                    {
                        fmain.dataGridView1.Rows[i].Cells["testResult"].Value = "FAIL";
                        fmain.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Pink;
                        fmain.result = false;
                    }
                }
            }
            catch (Exception eee)
            {
                MessageBox.Show(eee.Source + eee.Message);
            }
            
        }
    }
}

